﻿@model IEnumerable<StockShark.Models.OwnedInstrument>

@{
    ViewBag.Title = ViewBag.Header = "Nowe zlecenie sprzedaży";
}

<h4>Wybór waloru</h4>
<div class="editor-label">
@Html.Label("type", "Typ instrumentu")
</div>
<div class="editor-field">
@Html.DropDownList("type", new SelectList(ViewBag.TypeList))
</div>
@using (Html.BeginForm("Sell", "Order", FormMethod.Get))
{
<div class="editor-label">
@Html.Label("instrument", "Walor")
</div>
<div class="editor-field">
<select id="item" name="item"></select><div id="iteminfo"></div>
</div>
@Html.Hidden("account", (string)ViewBag.BrokerageAccount.Name)
<p>
    <input type="submit" value="Dalej" />
</p>
}

<script type="text/javascript">
$(function () {

    @foreach (var i in Model)
    {            
        @:$("select#item").append("<option value='@i.Paper.Id' title='Posiadane: @i.Quantity; aktualna cena: @i.Paper.EstimatedPrice PLN'>@i.Paper.Name</option>");
    }
    $("#iteminfo").append($("select#item option:selected").attr("title"));
    
    $("select#item").change(function () {
        $("#iteminfo").empty();
        $("#iteminfo").append($("select#item option:selected").attr("title"));
    });

    $("select#type").change(function () {
        $("select#item").empty();
        if ($("select#type option:selected").text() == "Wszystko")
        {
            @foreach (var i in Model)
            {            
                @:$("select#item").append("<option value='@i.Paper.Id' title='Posiadane: @i.Quantity; aktualna cena: @i.Paper.EstimatedPrice PLN'>@i.Paper.Name</option>");
            }
        }
        else if ($("select#type option:selected").text() == "Akcje - Rynek Główny")
        {
            @foreach (var i in Model.Where(r => r.Paper.GetType().Name == "Share" && r.Paper.Market == "GPW Rynek Główny"))
            {            
                @:$("select#item").append("<option value='@i.Paper.Id' title='Posiadane: @i.Quantity; aktualna cena: @i.Paper.EstimatedPrice PLN'>@i.Paper.Name</option>");
            }
        }
        else if ($("select#type option:selected").text() == "Akcje - NewConnect")
        {
            @foreach (var i in Model.Where(r => r.Paper.GetType().Name == "Share" && r.Paper.Market == "NewConnect"))
            {            
                @:$("select#item").append("<option value='@i.Paper.Id' title='Posiadane: @i.Quantity; aktualna cena: @i.Paper.EstimatedPrice PLN'>@i.Paper.Name</option>");
            }
        }
        else if ($("select#type option:selected").text() == "Waluty")
        {
            @foreach (var i in Model.Where(r => r.Paper.GetType().Name == "Currency"))
            {            
                @:$("select#item").append("<option value='@i.Paper.Id' title='Posiadane: @i.Quantity; aktualna cena: @i.Paper.EstimatedPrice PLN'>@i.Paper.Name</option>");
            }
        }
        else if ($("select#type option:selected").text() == "Surowce")
        {
            @foreach (var i in Model.Where(r => r.Paper.GetType().Name == "Material"))
            {            
                @:$("select#item").append("<option value='@i.Paper.Id' title='Posiadane: @i.Quantity; aktualna cena: @i.Paper.EstimatedPrice PLN'>@i.Paper.Name</option>");
            }
        }   
        $("#iteminfo").empty();
        $("#iteminfo").append($("select#item option:selected").attr("title"));                    
    });
});
</script>